package com.zzx.bms.controller;

import com.zzx.bms.domain.Department;
import com.zzx.bms.domain.DepartmentType;
import com.zzx.bms.domain.User;
import com.zzx.bms.service.IDepartmentService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SamZhao on 2016/12/6.
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {

    private static Logger logger = Logger.getLogger(DepartmentController.class);

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    public String departmentList(Department department, Model model) throws Exception {

        List<Department> deplist = departmentService.findAllDepartmentList();
        List<DepartmentType> typelist = departmentService.findAllDepartmentTypeList();

        List<Department> deplist1 = getDepList(deplist, typelist);

        model.addAttribute("deplist", deplist1);

        return "department/departmentList";
    }

    private List<Department> getDepList(List<Department> deplist, List<DepartmentType> typelist) {
        List<Department> result = new ArrayList<Department>();
        for(Department temp : deplist) {
            if (temp.getTypeid() != null) {
                for (DepartmentType deptemp : typelist) {
                    if (deptemp.getId() == temp.getTypeid()) {
                        temp.setType(deptemp.getName());
                        break;
                    }
                }
            }
            result.add(temp);
        }
        return result;
    }

    @RequestMapping("/listType")
    public String departmentTypeList(DepartmentType departmentType, Model model) throws Exception {

        List<DepartmentType> typelist = departmentService.findAllDepartmentTypeList();

        model.addAttribute("typelist", typelist);

        return "department/departmentTypeList";
    }

    @RequestMapping(value = "/addDepartment")
    public String addDepartment(Model model){
        List<DepartmentType> typelist = departmentService.findAllDepartmentTypeList();

        model.addAttribute("typelist", typelist);
        return "department/addDepartment";
    }

    @RequestMapping("/saveDepartment")
    public @ResponseBody String saveDepartment(Department department) throws Exception {
        int flag = departmentService.saveDepartment(department);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("/deleteDepartment")
    public @ResponseBody String deleteDepartment(Department department, String id) throws Exception {
        int flag = departmentService.deleteDepartmentById(id);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping(value = "/checkDepartment")
    public @ResponseBody String checkDepartment(Model model, Department department){
        int flag = departmentService.checkDepartment(department);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping(value = "/editDepartment")
    public String editDepartment(Model model, String id){
        Department department = departmentService.findDepartmentById(id);
        List<DepartmentType> typelist = departmentService.findAllDepartmentTypeList();
        model.addAttribute("department", department);
        model.addAttribute("typelist", typelist);
        return "department/editDepartment";
    }

    @RequestMapping(value = "/addDepartmentType")
    public String addDepartmentType(Model model){
        return "department/addDepartmentType";
    }

    @RequestMapping("/saveDepartmentType")
    public @ResponseBody String saveDepartmentType(DepartmentType departmentType) throws Exception {
        int flag = departmentService.saveDepartmentType(departmentType);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("/deleteDepartmentType")
    public @ResponseBody String deleteDepartmentType(DepartmentType departmentType, String id) throws Exception {
        int flag = departmentService.deleteDepartmentTypeById(id);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping(value = "/checkDepartmentType")
    public @ResponseBody String checkDepartmentType(Model model, DepartmentType departmentType){
        int flag = departmentService.checkDepartmentType(departmentType);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping(value = "/editDepartmentType")
    public String editDepartmentType(Model model, String id){
        DepartmentType departmentType = departmentService.findDepartmentTypeById(id);
        model.addAttribute("departmentType", departmentType);
        return "department/editDepartmentType";
    }

}
